<?php
declare (strict_types = 1);

namespace app\school\service\school\admin;

use app\admin\logic\system\Config as SystemConfig;
use app\common\service\BaseService;
use app\school\logic\school\admin\SchoolLevel as SchoolLevelLogic;
use eduline\admin\libs\pageform\FormItem;
use eduline\admin\libs\pagelist\ListItem;
use eduline\admin\page\PageForm;
use eduline\admin\page\PageList;
use think\facade\Request;
use think\Response;

/**
 * 机构等级管理-后台
 */
class SchoolLevel extends BaseService
{
    /**
     * 机构等级列表
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-22
     *
     * @return Response [type]                         [description]
     */
    public function index(): Response
    {
        // 搜索功能
        $map  = [];
        $data = app(SchoolLevelLogic::class)->getList($map);
        // 定义字段
        $keyList = [
            'title'           => ListItem::make()->title('等级名称'),
            'min_sales_price' => ListItem::make()->title('等级最低销售额'),
            'proportion'      => ListItem::make()->title('分成百分比'),
            'update_time'     => ListItem::make()->title('最后操作时间'),
        ];

        // 设置表单
        $list = app(PageList::class);
        // 表单字段
        $list->pageKey = $keyList;

        $list->datas = $data;

        return $list->send();
    }

    /**
     * 新建
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-22
     *
     * @return Response [type]                                  [description]
     */
    public function create(): Response
    {
        if (Request::isPost()) {
            $data = Request::post();
            $re   = app(SchoolLevelLogic::class)->createLevel($data);
            return $this->parseToData($re->toArray(), 1, '添加成功');
        }

        /* 创建课程界面配置 */
        $keyList = $this->keyListInit();
        // 设置表单
        $form = app(PageForm::class);
        // 表单字段
        $form->pageKey = $keyList;

        $form->saveMethod  = 'POST';
        $url               = '/school/admin/level/create';
        $form->savePostUrl = $url;

        return $form->send();

    }

    /**
     * 编辑
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-22
     *
     * @param    [type]                         $id [description]
     * @return Response [type]                             [description]
     */
    public function edit($id): Response
    {
        if (Request::isPut()) {
            $data  = Request::put();
            $video = app(SchoolLevelLogic::class)->editLevel($id, $data);

            return $this->parseToData($video->toArray(), 1, '编辑成功');
        }

        $data = app(SchoolLevelLogic::class)->findOrFail($id);
        // 创建课程界面配置
        $keyList = $this->keyListInit();
        // 设置表单
        $form = app(PageForm::class);
        // 表单字段
        $form->datas       = $data;
        $form->pageKey     = $keyList;
        $form->saveMethod  = 'PUT';
        $form->savePostUrl = '/school/admin/level/' . $id . '/edit';

        return $form->send();
    }

    /**
     * 表单字段初始化
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-22
     */
    protected function keyListInit(): array
    {
        // 分成比例
        $key    = 'split.config';
        $config = SystemConfig::get($key, ['platform'=>0, 'school'=>0]);
        $max    = $config['platform']??0 + $config['school']??0;
        // 定义字段
        return [
            'title'           => FormItem::make()->title('等级名称')->maxlength(20)->required(),
            'min_sales_price' => FormItem::make('inputNumber')->title('最低销售额')->min(0.01)->precision(2)->required(),
            'proportion'      => FormItem::make()->title('分成比例')->slot('%')->css(['width' => '156px'])->required()->help('1. 机构分成比例不能超过平台与机构比例之和<br />2. 若机构的分成比例增大，则平台的分成比例就相应减少<br />3. 当前可配置比例区间为 <font style="color:red;">[0,' . $max . ']</font>'),
        ];
    }

    /**
     * 删除等级
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-22
     *
     * @param int $id [description]
     * @return Response [type]                             [description]
     */
    public function delete(int $id): Response
    {
        $res = app(SchoolLevelLogic::class)->deleteLevel($id);

        return $this->parseToData($res, 1, '删除成功');
    }
}
